package com.panda.command;

import com.panda.common.FileHandler;
import com.panda.git.Packetizer;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class PushOperation extends Operation{


    private Argument root = EmptyArgument.getInstance();
    {
        root.next(new SimpleArgument("f", "input")).next(new SimpleArgument("group", "group")).next(new SimpleArgument("root", "root")).next(new SimpleArgument("version", "version"));
    }



    @Override
    boolean canHandle(String ope) {
        return ope.equals("push");
    }

    @Override
    void doHandle(String ope, List<String> args) throws Exception {


        Map<String, String> as = new HashMap<>();

        args.forEach(s -> root.handle(as, s));

        String input = as.get("input");

        if (input == null) {
            throw new RuntimeException("未指定文件输入,使用 -f 参数指定项目路径");
        }


        Packetizer packetizer = new Packetizer(new FileHandler(new File(input), Console.charset));

        packetizer.packet(as.get("group"),as.get("root"),as.get("version"));

        Console.log("打包成功,项目空间:" + as.get("group") + "/" + as.get("root") + "/" + as.get("version"));


    }
}
